﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using RestSharp;
using Newtonsoft.Json;

namespace OngageApiTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String userName = "erik@talentavenue.com";
            String password = "aveSFtalent";
            String accountCode = "talent_avenue";
            String listId = "1353";
            String url = "http://connect.ongage.net";
            
            String urlContacts = url + "/api/contacts";

            ///////////////Using Restsharp + Newtonsoft.Json

            //test look up
            var client = new RestSharp.RestClient(url);
            var request = new RestRequest("/api/contacts/lookup", Method.POST);

            String jsonToSend = @" { ""contact_id"": """ + contactId + @""" }";


            request.AddHeader("X_USERNAME", userName);
            request.AddHeader("X_PASSWORD", password);
            request.AddHeader("X_ACCOUNT_CODE", accountCode);
            request.AddParameter("application/json; charset=utf-8", jsonToSend, ParameterType.RequestBody);
            request.RequestFormat = DataFormat.Json;
            try
            {
                client.ExecuteAsync(request, response =>
                {
                    MessageBox.Show(response.Content);
                    //Result deserializedProduct = JsonConvert.DeserializeObject<Result>(response.Content);
                    //if (response.StatusCode == HttpStatusCode.OK)
                    //{
                    //    //OK
                    //    MessageBox.Show(deserializedProduct.payload._id);
                    //}
                    //else
                    //{
                    //    //NOK
                    //    MessageBox.Show(deserializedProduct.payload.message);
                    //}
                });
            }
            catch (Exception error)
            {
                //log
            }

            //var client = new RestSharp.RestClient(url);
            //var request = new RestRequest("/api/contacts", Method.POST);
            ////string jsonToSend = @"{""email"":""hai11@yahoo.com"",""list_id"":1353,""fields"":{""email"":""hai11@yahoo.com"",""address"":"""",""country"":"""",""first_name"":"""",""last_name"":"""",""gender"":"""",""ip"":"""",""language"":"""",""phone"":"""",""os"":"""",""product_id"":""""}}";
            //OngageContact oc = new OngageContact();
            //oc.email = "hai16@yahoo.com";
            //oc.list_id = listId;
            //oc.fields = new OngageApiTest.OngageContact.Fields();

            //string jsonToSend = JsonConvert.SerializeObject(oc);

            //request.AddHeader("X_USERNAME", userName);
            //request.AddHeader("X_PASSWORD", password);
            //request.AddHeader("X_ACCOUNT_CODE", accountCode);
            //request.AddParameter("application/json; charset=utf-8", jsonToSend, ParameterType.RequestBody);
            //request.RequestFormat = DataFormat.Json;
            //try
            //{
            //    client.ExecuteAsync(request, response =>
            //    {
            //        Result deserializedProduct = JsonConvert.DeserializeObject<Result>(response.Content);
            //        if (response.StatusCode == HttpStatusCode.OK)
            //        {
            //            //OK
            //            MessageBox.Show(deserializedProduct.payload._id);
            //        }
            //        else
            //        {
            //            //NOK
            //            MessageBox.Show(deserializedProduct.payload.message);
            //        }
            //    });
            //}
            //catch (Exception error)
            //{
            //    //log
            //}

            ///////////////////Not use Restsharp
            //String url = "http://connect.ongage.net/";

            //String urlContacts = url + "api/contacts";
            //HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(urlContacts);
            //hwr.Method = "POST";
            //hwr.Headers.Add("X_USERNAME", userName);
            //hwr.Headers.Add("X_PASSWORD", password);
            //hwr.Headers.Add("X_ACCOUNT_CODE", accountCode);
            //hwr.ContentType = "text/plain";

            //byte[] bytes = Encoding.ASCII.GetBytes(@"{""email"":""hai10@yahoo.com"",""list_id"":1353,""fields"":{""email"":""hai10@yahoo.com"",""address"":"""",""country"":"""",""first_name"":"""",""last_name"":"""",""gender"":"""",""ip"":"""",""language"":"""",""phone"":"""",""os"":"""",""product_id"":""""}}");
            //hwr.ContentLength = bytes.Length;

            //Stream oStreamOut = hwr.GetRequestStream();
            //oStreamOut.Write(bytes, 0, bytes.Length);
            //oStreamOut.Close();

            //try
            //{
            //    using (HttpWebResponse hwre = (HttpWebResponse)hwr.GetResponse())
            //    {
            //    }

            //}
            //catch (Exception)
            //{
                
            //    //throw;
            //}
        }
    }
}
